/**
 * @fileOverview The "resize" plugin.
 */
VED.plugins.add( 'resize', {
   init: function( editor ) {
      var config = editor.config;
      var spaceId = editor.ui.spaceId( 'resizer' );

      // Resize in the same direction of chrome,
      // which is identical to dir of editor element. (#6614)
      var resizeDir = editor.element ? editor.element.getDirection( 1 ) : 'ltr';

      !config.resize_dir && ( config.resize_dir = 'vertical' );
      ( config.resize_maxWidth == undefined ) && ( config.resize_maxWidth = 3000 );
      ( config.resize_maxHeight == undefined ) && ( config.resize_maxHeight = 3000 );
      ( config.resize_minWidth == undefined ) && ( config.resize_minWidth = 750 );
      ( config.resize_minHeight == undefined ) && ( config.resize_minHeight = 250 );

      if ( config.resize_enabled !== false ) {
         var container = null,
            origin, startSize,
            resizeHorizontal = ( config.resize_dir == 'both' || config.resize_dir == 'horizontal' ) && ( config.resize_minWidth != config.resize_maxWidth ),
            resizeVertical = ( config.resize_dir == 'both' || config.resize_dir == 'vertical' ) && ( config.resize_minHeight != config.resize_maxHeight );

         function dragHandler( evt ) {
            var dx = evt.data.$.screenX - origin.x,
               dy = evt.data.$.screenY - origin.y,
               width = startSize.width,
               height = startSize.height,
               internalWidth = width + dx * ( resizeDir == 'rtl' ? -1 : 1 ),
               internalHeight = height + dy;

            if ( resizeHorizontal )
               width = Math.max( config.resize_minWidth, Math.min( internalWidth, config.resize_maxWidth ) );

            if ( resizeVertical )
               height = Math.max( config.resize_minHeight, Math.min( internalHeight, config.resize_maxHeight ) );

            // DO NOT impose fixed size with single direction resize. (#6308)
            editor.resize( resizeHorizontal ? width : null, height );
         }

         function dragEndHandler( evt ) {
            VED.document.removeListener( 'mousemove', dragHandler );
            VED.document.removeListener( 'mouseup', dragEndHandler );

            if ( editor.document ) {
               editor.document.removeListener( 'mousemove', dragHandler );
               editor.document.removeListener( 'mouseup', dragEndHandler );
            }
         }

         var mouseDownFn = VED.tools.addFunction( function( $event ) {
            if ( !container )
               container = editor.getResizable();

            startSize = { width: container.$.offsetWidth || 0, height: container.$.offsetHeight || 0 };
            origin = { x: $event.screenX, y: $event.screenY };

            config.resize_minWidth > startSize.width && ( config.resize_minWidth = startSize.width );
            config.resize_minHeight > startSize.height && ( config.resize_minHeight = startSize.height );

            VED.document.on( 'mousemove', dragHandler );
            VED.document.on( 'mouseup', dragEndHandler );

            if ( editor.document ) {
               editor.document.on( 'mousemove', dragHandler );
               editor.document.on( 'mouseup', dragEndHandler );
            }

            $event.preventDefault && $event.preventDefault();
         });

         editor.on( 'destroy', function() {
            VED.tools.removeFunction( mouseDownFn );
         });

         editor.on( 'uiSpace', function( event ) {
            if ( event.data.space == 'bottom' ) {
               var direction = '';
               if ( resizeHorizontal && !resizeVertical )
                  direction = ' ved_resizer_horizontal';
               if ( !resizeHorizontal && resizeVertical )
                  direction = ' ved_resizer_vertical';

               var resizerHtml =
                  '<span' +
                  ' id="' + spaceId + '"' +
                  ' class="ved_resizer' + direction + ' ved_resizer_' + resizeDir + '"' +
                  ' title="' + VED.tools.htmlEncode( editor.lang.common.resize ) + '"' +
                  ' onmousedown="VED.tools.callFunction(' + mouseDownFn + ', event)"' +
                  '>' +
                  // BLACK LOWER RIGHT TRIANGLE (ltr)
                  // BLACK LOWER LEFT TRIANGLE (rtl)
                  ( resizeDir == 'ltr' ? '\u25E2' : '\u25E3' ) +
                  '</span>';

               // Always sticks the corner of botttom space.
               resizeDir == 'ltr' && direction == 'ltr' ? event.data.html += resizerHtml : event.data.html = resizerHtml + event.data.html;
            }
         }, editor, null, 100 );

         // Toggle the visibility of the resizer when an editor is being maximized or minimized.
         editor.on( 'maximize', function( event ) {
            editor.ui.space( 'resizer' )[ event.data == VED.TRISTATE_ON ? 'hide' : 'show' ]();
         });
      }
   }
});



